package com.base;

import android.os.Build;
import android.os.Bundle;
import android.view.Gravity;
import android.view.View;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import androidx.annotation.LayoutRes;
import androidx.annotation.Nullable;
import androidx.annotation.StringRes;
import androidx.appcompat.app.AppCompatActivity;

import com.demo.view.LoadingDialog;
import com.module.commonui.R;
import com.tange.base.toolkit.StatusNavUtils;
import com.tg.appcommon.android.ResourcesUtil;
import com.tange.base.toolkit.StringUtils;
import com.tg.appcommon.android.TGApplicationBase;
import com.tg.appcommon.helper.ConnectivityManagerHelper;

public abstract class BaseActivity extends AppCompatActivity {
    LoadingDialog mLoadingDialog;
    public static final String SETTING_ITEM_TITLE = "device_setting_item_title";
    protected String mTitle;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (getIntent() != null) {
            mTitle = getIntent().getStringExtra(SETTING_ITEM_TITLE);
        }
        if (shouldSetStatusBarColor()) {
            setStatusBarColor();
        }
    }

    public boolean shouldSetStatusBarColor() {
        return false;
    }

    protected void setStatusBarColor() {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
            StatusNavUtils.setStatusBarColor(this, statusBarColor());
        } else {
            StatusNavUtils.setStatusBarColor(this, statusBarColor());
        }
    }

    protected int statusBarColor() {
        return getResources().getColor(R.color.play_background);
    }

    @Override
    public void setContentView(@LayoutRes int layoutResID) {
        super.setContentView(layoutResID);
        if (!StringUtils.isEmpty(mTitle)) {
            modifyToolBar(mTitle);
        }
    }

    protected void hideActionBar() {
        if (getSupportActionBar() != null) {
            getSupportActionBar().hide();
        }
    }

    protected abstract void initView();

    protected void setClickBack(@Nullable View.OnClickListener listener) {
        View view = findViewById(R.id.iv_back_toolbar);
        if (view == null) {
            view = findViewById(R.id.back_toolbar);
        }
        if (view != null) {
            if (listener != null) {
                view.setOnClickListener(listener);
            } else {
                view.setOnClickListener(v -> finish());
            }
        }
    }

    //新的需求，更改颜色
    protected void modifyToolBar(String deviceName, boolean showTitle) {
        RelativeLayout toolbar = findViewById(R.id.toolbar);
        if (toolbar != null) {
            toolbar.setBackgroundColor(statusBarColor());
            TextView title = toolbar.findViewById(R.id.device_name);
            if (title != null) {
                if (!showTitle) {
                    title.setVisibility(View.GONE);
                } else {
                    title.setVisibility(View.VISIBLE);
                    title.setTextColor(getResources().getColor(R.color.global_customize_fixed_color_black));
                    if (!StringUtils.isEmpty(deviceName)) {
                        title.setText(deviceName);
                        mTitle = deviceName;
                    }
                }
            }

        }
    }

    //新的需求，更改颜色
    protected void modifyToolBar(String deviceName) {
        modifyToolBar(deviceName, true);
    }

    protected void modifyToolBar(@StringRes int resid) {
        modifyToolBar(ResourcesUtil.getString(resid), true);
    }

    protected void modifyToolBar() {
        modifyToolBar(null);
    }

    protected void showLoading() {
        if (mLoadingDialog == null) {
            mLoadingDialog = new LoadingDialog(this);
            mLoadingDialog.setMsg(R.string.loading);
        }
        if (!isFinishing() && !isDestroyed()) {
            mLoadingDialog.show();
        }
    }

    protected void showLoading(String message) {
        if (mLoadingDialog == null) {
            mLoadingDialog = new LoadingDialog(this);
            mLoadingDialog.setMsg(message);
        }
        if (!isFinishing() && !isDestroyed()) {
            mLoadingDialog.show();
        }
    }

    protected void hideLoading() {
        if (mLoadingDialog != null && mLoadingDialog.isShowing()) {
            mLoadingDialog.cancel();
        }
    }

    protected void showToast(final String msg) {
        runOnUiThread(() -> Toast.makeText(TGApplicationBase.getApplication(), msg, Toast.LENGTH_SHORT).show());
    }

    protected void showToast(final int resId) {
        runOnUiThread(() -> Toast.makeText(TGApplicationBase.getApplication(), resId, Toast.LENGTH_SHORT).show());
    }

    protected void showLongToast(final int resId, int topMargin) {
        runOnUiThread(() -> {
            try {
                Toast toast = Toast.makeText(TGApplicationBase.getApplication(), resId, Toast.LENGTH_LONG);
                toast.setGravity(Gravity.TOP, 0, topMargin);
                toast.show();
            } catch (Throwable ex) {
                showToast(resId);
            }

        });
    }

    @Override
    protected void onResume() {
        super.onResume();
        ConnectivityManagerHelper.getInstance().register();
    }


    @Override
    protected void onPause() {
        super.onPause();
        ConnectivityManagerHelper.getInstance().unregister();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();

        if (mLoadingDialog != null && mLoadingDialog.isShowing()) {
            mLoadingDialog.cancel();
        }
        mLoadingDialog = null;
    }

}
